package me.ezitku.base.popupwindow

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import com.ug_project.views.findTextView

import me.ezitku.base.R


/**
 * Created by Administrator on 2016/6/6 0006.
 */
class CalcView(context: Context, private val mTextView: TextView) : PopupWindow() {

    private val conentView: View
    private var text = ""
    private var orginalText = ""

    init {
        this.orginalText = mTextView.text.toString()
        val inflater = context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        conentView = inflater.inflate(R.layout.calcula_layout, null)
        val viewHolder = ViewHolder(conentView)
        conentView.tag = viewHolder
        // 设置SelectPicPopupWindow的View
        this.contentView = conentView
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.width = context.resources.getDimensionPixelSize(R.dimen.popup_calcView_width)
        // 设置SelectPicPopupWindow弹出窗体的高
        this.height =/*context.resources.getDimensionPixelSize(R.dimen.popup_calcView_height)*/ ViewGroup.LayoutParams.WRAP_CONTENT
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.isFocusable = false
        this.isOutsideTouchable = false
        // 刷新状态
        this.update()
        // 实例化一个ColorDrawable颜色为半透明
        val dw = ColorDrawable(0)
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw)
        //         mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        //this.animationStyle = R.style.AnimationPreview
        val listener = NumberListener()
        viewHolder.btn_1!!.setOnClickListener(listener)
        viewHolder.btn_2!!.setOnClickListener(listener)
        viewHolder.btn_3!!.setOnClickListener(listener)
        viewHolder.btn_4!!.setOnClickListener(listener)
        viewHolder.btn_5!!.setOnClickListener(listener)
        viewHolder.btn_6!!.setOnClickListener(listener)
        viewHolder.btn_7!!.setOnClickListener(listener)
        viewHolder.btn_8!!.setOnClickListener(listener)
        viewHolder.btn_9!!.setOnClickListener(listener)
        viewHolder.btn_0!!.setOnClickListener(listener)
        viewHolder.btn_point!!.setOnClickListener(PointListener())
        viewHolder.btn_del!!.setOnClickListener(DeleteListener())
    }

    internal inner class NumberListener : View.OnClickListener {

        override fun onClick(v: View) {
            val textView = v as TextView
            text = text + textView.text.toString()
            mTextView.text = text
        }
    }

    internal inner class PointListener : View.OnClickListener {

        override fun onClick(v: View) {
            if (text != "") {
                val textView = v as TextView
                text = text + textView.text.toString()
                mTextView.text = text
            }
        }
    }

    internal inner class DeleteListener : View.OnClickListener {

        override fun onClick(v: View) {
            if (text != "") {
                text = text.substring(0, text.length - 1)
                mTextView.text = text
            }
        }
    }

    override fun dismiss() {
        try {
            java.lang.Float.parseFloat(text)
        } catch (e: Exception) {
            mTextView.text = orginalText
        }

        super.dismiss()
    }

    /**
     * 显示popupWindow

     * @param parent
     */
    fun showPopupWindow(parent: View) {
        if (!this.isShowing) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent)/*, parent.width / 2, -parent.height / 2 - parent.resources
                    .getDimensionPixelSize(R.dimen.popup_calcView_height) / 2)*/
        } else {
            this.dismiss()
        }
    }

    internal inner class ViewHolder(view: View) {
        var btn_1: TextView? = null
        var btn_2: TextView? = null
        var btn_3: TextView? = null
        var btn_4: TextView? = null
        var btn_5: TextView? = null
        var btn_6: TextView? = null
        var btn_7: TextView? = null
        var btn_8: TextView? = null
        var btn_9: TextView? = null
        var btn_0: TextView? = null
        var btn_point: TextView? = null
        var btn_del: ImageView? = null

        init {
            btn_1 = view.findTextView(R.id.btn_1)
            btn_2 = view.findTextView(R.id.btn_2)
            btn_3 = view.findTextView(R.id.btn_3)
            btn_4 = view.findTextView(R.id.btn_4)
            btn_5 = view.findTextView(R.id.btn_5)
            btn_6 = view.findTextView(R.id.btn_6)
            btn_7 = view.findTextView(R.id.btn_7)
            btn_8 = view.findTextView(R.id.btn_8)
            btn_9 = view.findTextView(R.id.btn_9)
            btn_0 = view.findTextView(R.id.btn_0)
            btn_point = view.findTextView(R.id.btn_point)
            btn_del = view.findViewById(R.id.btn_del)
        }
    }
}
